<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
        	height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }
        .content {
        	height: 100%;
        	padding: 0 0;
        	display: -webkit-box;
        	-webkit-box-align: end;
        }
        .main {
        	width: 100%;
        	background-color: #fff;
        	overflow: hidden;
        }
        .main .main_t {
        	height: 50px;
        	line-height: 50px;
        	font-size: 16px;
        	color: #333;
        	text-align: center;
        	border-bottom: 1px solid #e8e8e8;
        }
        .main .main_b {
        	padding: 15px;
            padding-bottom: 0;
            text-align: center;
        }
        .main_b img{
            margin: 0 auto;
            display: inline-block;
            width: 200px;
        }
        .btn_box{
            line-height: 60px;
            color: #40afe2;
        }
        .main .main_t.cancel{
            color: #e62442;
        }
    </style>
</head>
<body>
	<div class="content" id="dialog">
	    <div class="main">
            <div tapmode onclick="toCamera('camera');" class="main_t camera">拍照</div>
            <div tapmode onclick="toCamera('album');" class="main_t phone">从相册选择</div>
	    	<div tapmode onclick="toCancel();" class="main_t cancel">取消</div>
	    </div>
	</div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
    apiready = function(){
        $(document.body).on('touchend', function(e) {
            var dialog = $('#dialog')[0];
            if (!$.contains(dialog, e.target)) {
                $('#dialog').removeClass('in');
                api.closeFrame();
            }
        });
    };

    // 取消
    function toCancel() {
        api.closeFrame();
    }

    function toCamera (_type) {
        api.getPicture({
            sourceType: _type,
            encodingType: 'jpg',
            mediaValue: 'pic',
            destinationType: 'url',
            allowEdit: true,
            preview:true,
            quality: 60,
            targetWidth: 400,
            // targetHeight: 100,
            saveToPhotoAlbum: false
        }, function(ret, err) {
            if (ret) {
                if(ret.data){
                    ajaxUploadPic(ret.data);
                }
            }
        });
    };

    function ajaxUploadPic (_data) {
        openLoading();
        var staffData = $api.getStorage('staff_data');
        var from = api.pageParam.from;
        api.ajax({
            url: rootPath+'/api/staff/update_photo',
            method: 'post',
            cache:false,
            data: {
                values: { 
                    staff_id: staffData.id,
                    staff_token: staffData.token,
                    from: from
                },
                files: { 
                    photo: _data
                }
            }
        },function(ret, err){
            if(ret.status == 200){
                if(from == 'head'){
                    api.sendEvent({
                        name: 'changeApplyStaffHead',
                        extra: {
                            path: ret.path, 
                        }
                    });
                }
                if(from == 'front' || from == 'reverse' || from == 'drive_front' || from == 'drive_reverse'){
                    api.execScript({
                        name: 'application',
                        frameName: 'application_frame',
                        script: 'evChangeApplyIdcardFront("'+ret.path+'","'+from+'");'
                    });
                }
                
                closeLoading();
                setTimeout(function(){
                    api.closeFrame();
                },300);
            }else{
                closeLoading();
                setTimeout(function(){
                    api.closeFrame();
                },300);
            }
        });
    };


</script>
</html>